﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.service;
using WindowsForms.Class;

namespace WindowsForms
{
    public partial class StaffActionAEProcess : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        private List<Roles> lstRolesSource = null;

        public StaffActionAEProcess(bool? isModifyer = null)
        {
            iQuery = new Service1();
            ConvertToList convert = new ConvertToList();
            lstRolesSource = convert.ConvertTolistRoles(iQuery.SelectRolesAll().Tables[0]);

            InitializeComponent();

            cbRoll.Properties.DataSource = lstRolesSource;

            if (isModifyer.HasValue)
            {
                GenerateModifyerControl(isModifyer.Value);
                txtUsername.Enabled = false;
            }
        }

        private void GenerateModifyerControl(bool isModifyer)
        {
            if (isModifyer)
            {
                this.layoutProcessGroups.Text = "Chỉnh sửa thông tin Nhân Viên";
                this.layoutProcessGroups.CustomizationFormText = "Chỉnh sửa thông tin Nhân Viên";
                this.layoutProcessGroups.CaptionImage = global::WindowsForms.Properties.Resources.Edit;
            }
        }
        
        public string UserName
        {
            get
            {
                return txtUsername.Text;
            }
            set
            {
                txtUsername.Text = value;
            }
        }

        public string IMEI
        {
            get
            {
                return textEditImei.Text;
            }
            set
            {
                textEditImei.Text = value;
            }
        }

        public string Password
        {
            get
            {
                if (!String.IsNullOrWhiteSpace(txtPassword.Text))
                {
                    Authentication auth = new Authentication();
                    return auth.Encrypt(txtPassword.Text);   
                }
                return string.Empty;
            }            
        }

        public string StaffName
        {
            get
            {
                return txtName.Text;
            }
            set
            {
                txtName.Text = value;
            }
        }

        public string Address
        {
            get
            {
                return txtAddress.Text;
            }
            set
            {
                txtAddress.Text = value;
            }
        }

        public DateTime BirthDate
        {
            get
            {
                return DateTime.Parse(txtBirthDate.Text);
            }
            set
            {
                txtBirthDate.Text = value.ToString();
            }
        }

        
        public bool Gender
        {
            get
            {
                return cbSex.SelectedIndex == 0 ? false : true;
            }
            set
            {
                cbSex.SelectedIndex = value ? 1 : 0;
            }
        }

        public string NoNumber
        {
            get
            {
                return txtIdNo.Text;
            }
            set
            {
                txtIdNo.Text = value;
            }
        }

        public string PhoneNumber
        {
            get
            {
                return txtPhone.Text;
            }
            set
            {
                txtPhone.Text = value;
            }
        }

        public DateTime StartWorkDate
        {
            get
            {
                return DateTime.Parse(txtDateFrom.Text);
            }
            set
            {
                txtDateFrom.Text = value.ToString();
            }
        }

        public DateTime EndWorkDate
        {
            get
            {
                return DateTime.Parse(txtDateTo.Text);
            }
            set
            {
                txtDateTo.Text = value.ToString();
            }
        }

        public Roles Role
        {
            get
            {
                return lstRolesSource.Find(delegate(Roles id)
                {
                    return id.RoleID == (int)cbRoll.EditValue;
                });
            }
        }

        public int RoleId
        {
            get
            {
                return (int)cbRoll.EditValue;
            }
            set
            {
                cbRoll.EditValue = value;
            }
        }

        public bool Enable
        {
            get
            {
                return chkEnable.Checked;
            }
            set
            {
                chkEnable.Checked = value;
            }
        }

        private void StaffActionAEProcess_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !dxValidationProvider1.Validate() && DialogResult == DialogResult.OK;
        }

        private void StaffActionAEProcess_Load(object sender, EventArgs e)
        {

        }
    }
}